//
//  NSString+GGQuery.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/2.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "NSString+GGQuery.h"

@implementation NSString (GGQuery)

-(NSDictionary *)GGParametersFromQueryString{
    NSArray *params = [self componentsSeparatedByString:@"&"];
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithCapacity:[params count]];
    for (NSString *param in params) {
        NSArray *pairs = [param componentsSeparatedByString:@"="];
        if (pairs.count == 2) {
            NSString *key = [pairs[0] GGStringByReplacingPercentEscapesUsingEncoding];
            NSString *value = [pairs[1] GGStringByReplacingPercentEscapesUsingEncoding];
            paramsDict[key] = value;
        }else if (pairs.count == 1){
            NSString *key = [[pairs firstObject] GGStringByReplacingPercentEscapesUsingEncoding];
            paramsDict[key] = @"";
        }
    }
    return [paramsDict copy];
}


- (NSString *)GGStringByAddingPercentEscapesUsingEncoding{
    NSCharacterSet *allowedCharactersSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"];
    return [self stringByAddingPercentEncodingWithAllowedCharacters:allowedCharactersSet];
}
//解码
- (NSString *)GGStringByReplacingPercentEscapesUsingEncoding{
    return [self stringByRemovingPercentEncoding];
}
@end
